#include "mobilefilelist.h"
#include "mobilefilewidget.h"

#include <QDebug>

const QString ITEM_ICON_PATH = ":/icon/";
const QStringList ITEM_ICON_LIST = {"pictureIcon.png",  "videoIcon.png",  "musicIcon.png",
                                    "documentIcon.png", "weChatIcon.png", "qqIcon.png"};
const QStringList ITEM_NAME_LIST = {"picture", "video", "music", "document", "wechat", "qq"};
const int WIDGET_WIDTH = 920;
const int WIDGET_HEIGHT = 510;
const int FILE_LIST_WIDGET_WIDTH = 852;
const int FILE_LIST_WIDGET_HEIGHT = 236;
const int STROAGE_WIDGET_WIDTH = 852;
const int STROAGE_WIDGET_HEIGHT = 175;
const int FILE_LIST_WIDGET_ITEM_WIDTH = 205;
const int FILE_LIST_WIDGET_ITEM_HEIGHT = 118;
const int STROAGE_WIDGET_ITEM_WIDTH = 170;
const int STROAGE_WIDGET_ITEM_HEIGHT = 68;

MobileFileList::MobileFileList(QWidget *parent) : QWidget(parent)
{
    initWidget();
    setItem();
    show();
}

MobileFileList::~MobileFileList() {}

void MobileFileList::setItemCount(const QMap<QString, int> &map)
{
    if (map.isEmpty()) {
        return;
    }
    QMap<QString, int>::const_iterator it;
    QMap<QString, MobileFileListItem *>::iterator itemIt = m_itemMap.begin();
    MobileFileListItem *item = nullptr;
    while (m_itemMap.end() != itemIt) {
        item = itemIt.value();
        it = map.find(itemIt.key());
        item->setItemCount(it.value());
        itemIt++;
    }
}

void MobileFileList::setChangeFontSize(double fontSize)
{

}

void MobileFileList::initWidget()
{
    setFixedSize(WIDGET_WIDTH, WIDGET_HEIGHT);

    QVBoxLayout *mainVLayout = new QVBoxLayout();

    QHBoxLayout *mobileFileListHLayout = new QHBoxLayout();
    QHBoxLayout *fileListWidgetHLayout = new QHBoxLayout();
    QHBoxLayout *mobileStorageHLayout = new QHBoxLayout();
    QHBoxLayout *storageWidgetHLayout = new QHBoxLayout();
    //手机文件列表
    QLabel *mobileFileListLabel = new QLabel();
    QFont font;
    font.setBold(true);
    mobileFileListLabel->setFont(font);
    mobileFileListLabel->setText(tr("Mobile file list")); // Mobile file list手机文件列表

    mobileFileListHLayout->addWidget(mobileFileListLabel);
    mobileFileListHLayout->setContentsMargins(60, 0, 0, 0);
    mobileFileListHLayout->setSpacing(0);
    mobileFileListHLayout->addStretch();

    //手机存储
    QLabel *mobileStorageLabel = new QLabel();
    mobileStorageLabel->setFont(font);
    mobileStorageLabel->setText(tr("Mobile storage")); // Mobile storage移动存储

    mobileStorageHLayout->addWidget(mobileStorageLabel);
    mobileStorageHLayout->setContentsMargins(60, 0, 0, 0);
    mobileStorageHLayout->setSpacing(0);
    mobileStorageHLayout->addStretch();

    //手机文件列表widget
    m_fileListWidget = new QListWidget(this);
    m_fileListWidget->setFixedSize(FILE_LIST_WIDGET_WIDTH, FILE_LIST_WIDGET_HEIGHT);

    fileListWidgetHLayout->addWidget(m_fileListWidget);
    fileListWidgetHLayout->setContentsMargins(44, 0, 0, 0);
    fileListWidgetHLayout->setSpacing(0);
    fileListWidgetHLayout->addStretch();

    //项目在视图中从左到右排列
    m_fileListWidget->setFlow(QListView::LeftToRight);
    //设置显示图标模式
    m_fileListWidget->setViewMode(QListView::IconMode);
    //自适应布局
    m_fileListWidget->setResizeMode(QListView::Adjust);
    //列表中的图标不可拖动
    m_fileListWidget->setMovement(QListView::Static);
    //去边框
    m_fileListWidget->setFrameShape(QListWidget::NoFrame);
    //不可选择项目
    //    m_fileListWidget->setSelectionMode(QListView::NoSelection);

    //手机存储widget
    m_storageWidget = new QListWidget(this);
    m_storageWidget->setFixedSize(STROAGE_WIDGET_WIDTH, STROAGE_WIDGET_HEIGHT);

    storageWidgetHLayout->addWidget(m_storageWidget);
    storageWidgetHLayout->setContentsMargins(60, 0, 0, 0);
    storageWidgetHLayout->setSpacing(0);
    storageWidgetHLayout->addStretch();

    //项目在视图中从左到右排列
    m_storageWidget->setFlow(QListView::LeftToRight);
    //设置显示图标模式
    m_storageWidget->setViewMode(QListView::IconMode);
    //自适应布局
    m_storageWidget->setResizeMode(QListView::Adjust);
    //列表中的图标不可拖动
    m_storageWidget->setMovement(QListView::Static);
    //去边框
    m_storageWidget->setFrameShape(QListWidget::NoFrame);

    mainVLayout->addLayout(mobileFileListHLayout);
    mainVLayout->addSpacing(28);
    mainVLayout->addLayout(fileListWidgetHLayout);
    mainVLayout->addSpacing(50);
    mainVLayout->addLayout(mobileStorageHLayout);
    mainVLayout->addSpacing(28);
    mainVLayout->addLayout(storageWidgetHLayout);
    mainVLayout->addStretch();
    mainVLayout->setSpacing(0);
    setLayout(mainVLayout);
}

void MobileFileList::setItem()
{
    for (int i = 0; i < 6; i++) {
        QListWidgetItem *item = new QListWidgetItem(m_fileListWidget);
        MobileFileListItem *mobileFileListItem = new MobileFileListItem();
        m_fileListWidget->setItemWidget(item, mobileFileListItem);
        m_fileListWidget->setGridSize(QSize(FILE_LIST_WIDGET_ITEM_WIDTH, FILE_LIST_WIDGET_ITEM_HEIGHT));
        item->setSizeHint(QSize(STROAGE_WIDGET_ITEM_WIDTH, STROAGE_WIDGET_ITEM_HEIGHT));
        mobileFileListItem->setFocusPolicy(Qt::NoFocus);
        mobileFileListItem->setItemCount(0);
        mobileFileListItem->setItemIcon(ITEM_ICON_PATH + ITEM_ICON_LIST.value(i));
        m_itemMap.insert(ITEM_NAME_LIST.value(i), mobileFileListItem);
        switch (i) {
        case 0:
            mobileFileListItem->setItemName(tr("Picture")); // Picture图片
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotPictureButtonClicked);
            break;
        case 1:
            mobileFileListItem->setItemName(tr("Video")); // Video视频
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotVideoButtonClicked);
            break;
        case 2:
            mobileFileListItem->setItemName(tr("Music")); // Music音乐
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotMusicButtonClicked);
            break;
        case 3:
            mobileFileListItem->setItemName(tr("Document")); // Document文档
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotDocumentButtonClicked);
            break;
        case 4:
            mobileFileListItem->setItemName(tr("WeChat")); // WeChat微信
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotWeChatButtonClicked);
            break;
        case 5:
            mobileFileListItem->setItemName(tr("QQ")); // QQ   QQ
            connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotQQButtonClicked);
            break;
        default:
            break;
        }
    }
    QListWidgetItem *item = new QListWidgetItem(m_storageWidget);
    MobileFileListItem *mobileFileListItem = new MobileFileListItem();
    m_storageWidget->setItemWidget(item, mobileFileListItem);
    item->setSizeHint(QSize(STROAGE_WIDGET_ITEM_WIDTH, STROAGE_WIDGET_ITEM_HEIGHT));
    mobileFileListItem->setItemName(tr("Mobile"));  // Mobile手机
    mobileFileListItem->setAllFile(tr("All File")); // All File所有文件
    mobileFileListItem->setItemIcon(":/icon/mobileFileIcon.png");
    connect(mobileFileListItem, &MobileFileListItem::clicked, this, &MobileFileList::slotMobileButtonClicked);

    setAutoFillBackground(true);
    setBackgroundRole(QPalette::Base);
}

void MobileFileList::slotPictureButtonClicked()
{
    Q_EMIT sigPictureClicked(FILE_LIST_TYPE::PICTURE);
}

void MobileFileList::slotVideoButtonClicked()
{
    Q_EMIT sigVideoClicked(FILE_LIST_TYPE::VIDEO);
}

void MobileFileList::slotMusicButtonClicked()
{
    Q_EMIT sigMusicClicked(FILE_LIST_TYPE::MUSIC);
}

void MobileFileList::slotDocumentButtonClicked()
{
    Q_EMIT sigDocumentClicked(FILE_LIST_TYPE::DOCUMENT);
}

void MobileFileList::slotWeChatButtonClicked()
{
    Q_EMIT sigWeChatClicked(FILE_LIST_TYPE::WECHAT);
}

void MobileFileList::slotQQButtonClicked()
{
    Q_EMIT sigQQClicked(FILE_LIST_TYPE::QQ);
}

void MobileFileList::slotMobileButtonClicked()
{
    Q_EMIT sigMobileStorageClicked("/");
}
